package cn.com.self.security.user.service.impl;

import cn.com.self.apis.portal.util.ICustomPage;
import cn.com.self.security.mapper.LoginRoleMapper;
import cn.com.self.security.user.domain.LoginRoleEntity;
import cn.com.self.security.user.service.ILoginRoleService;
import cn.com.selfsoft.smartcity.core.service.BaseServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.thymeleaf.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
* <p>
    * 系统角色 服务实现类
    * </p>
*
* @author chenyongren
* @since 2019-09-27
*/
@Service
public class LoginRoleServiceImpl extends BaseServiceImpl<LoginRoleEntity, String> implements ILoginRoleService {

    @Autowired
    private LoginRoleMapper sysRoleMapper;



    @Override
    public ICustomPage<LoginRoleEntity> selectPage(Map<String, Object> queryParams, Page pageable) {
        QueryWrapper<LoginRoleEntity> queryWrapper = new QueryWrapper<>();
        queryParams.keySet().forEach(key -> {
            if(("id").equals(key)){
                queryWrapper.eq("id", queryParams.get("id"));
            }else if("code".equals(key)) {
                queryWrapper.eq("code",queryParams.get("code"));
            }else if("name".equals(key)) {
                queryWrapper.like("name",queryParams.get("name"));
            }else if("parent".equals(key)) {
                queryWrapper.eq("parent",queryParams.get("parent")).or().eq("id", queryParams.get("parent"));

            }
        });
        queryWrapper.orderByDesc("id");
        ICustomPage<LoginRoleEntity> pageList =  (ICustomPage<LoginRoleEntity>)sysRoleMapper.selectPage(pageable,queryWrapper);
        return  pageList;
    }

    @Override
    public List<LoginRoleEntity> selectRoleList(Map<String, Object> queryParams) {
        String admin = (String) queryParams.get("is_admin");
        if(!StringUtils.isEmpty(admin) && admin.equals("1")){
            QueryWrapper<LoginRoleEntity> queryWrapper = new QueryWrapper<>();
            return this.sysRoleMapper.selectList(queryWrapper);
        }
        return sysRoleMapper.selectLoginRoleList(queryParams);
    }

    /*@Override
    public PageImpl<SysRoleEntity> selectRoleList(Map<String, Object> queryParams, Pageable page) {

        Integer total = sysRoleMapper.selectRoleListTotal(queryParams);
        if(total==null){
            total = 0;
        }
        queryParams.put("page", (page.getPageNumber()- 1) * page.getPageSize());
        queryParams.put("size", page.getPageSize());
        //List<OldExchangeNewVo> list = oldExchangeNewMapper.selectPages(parameterMap);
        List<SysRoleEntity> pageList =  sysRoleMapper.selectRoleList(queryParams);
        return new PageImpl(pageList, PageRequest.of(page.getPageNumber() - 1, page.getPageSize()), total);
    }
   */

}
